﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MbUnit.Framework;
using Foundation.ObjectHydrator.Interfaces;
using Foundation.ObjectHydrator.Generators;
using System.Text.RegularExpressions;
using System.Xml;

namespace Foundation.ObjectHydrator.Tests.GeneratorTests
{
    [TestFixture]
    public class Generator_Tests
    {
        [Test]
        public void FirstNameGeneratorTest()
        {
            IGenerator fng = new FirstNameGenerator();
            string firstname = (string)fng.Generate();
            Assert.IsNotNull(firstname);
        }

        [Test]
        public void LastNameGeneratorTest()
        {
            IGenerator lng = new LastNameGenerator();
            string lastname = (string)lng.Generate();
            Assert.IsNotNull(lastname);
        }

        [Test]
        public void CompanyNameGeneratorTest()
        {
            IGenerator cng = new CompanyNameGenerator();
            string companyname = (string)cng.Generate();
            Assert.IsNotNull(companyname);
        }

        [Test]
        public void DateTimeGeneratorWithDefaultTest()
        {
            IGenerator dtg = new DateTimeGenerator();
            DateTime checkme = (DateTime)dtg.Generate();
            DateTime current=DateTime.Now;
            Assert.IsNotNull(checkme);
            Assert.Between<DateTime>(checkme, current.AddYears(-10), current.AddYears(10));
        }

        [Test]
        public void DateTimeGeneratorWithOverrideValues()
        {
            
            DateTime mymin = Convert.ToDateTime("1/1/1972");
            DateTime mymax = Convert.ToDateTime("1/1/1980");

            IGenerator dtg = new DateTimeGenerator(mymin,mymax);
            DateTime checkme = (DateTime)dtg.Generate();
            Assert.IsNotNull(checkme);
            Assert.Between<DateTime>(checkme, mymin, mymax);
        }

        [Test]
        public void DefaultGenerator()
        {
            string setme = "hi";
            IGenerator defgen = new DefaultGenerator(setme);
            string checkme = (string)defgen.Generate();
            Assert.IsNotNull(checkme);
            Assert.AreEqual(checkme, setme);

        }

        [Test]
        public void DoubleGeneratorWithDefaultValues()
        {
            IGenerator doublegen = new DoubleGenerator();
            double checkme = (double)doublegen.Generate();
            Assert.IsNotNull(checkme);
            Assert.Between<double>(checkme, 0.00, 100.00);
        }

        [Test]
        public void EnumGeneratorTest()
        {
            //IGenerator enumgen = new EnumGenerator();

        }

        [Test]
        public void IntegerGeneratorWithDefaultTest()
        {
            IGenerator intgen = new IntegerGenerator();
            int checkme = (int)intgen.Generate();
            Assert.IsNotNull(checkme);
            Assert.Between<int>(checkme, 0, 100);
        }

        [Test]
        public void IntegerGeneratorWithOverrideTest()
        {
            int min = 5;
            int max = 20;
            IGenerator intgen = new IntegerGenerator(min,max);
            int checkme = (int)intgen.Generate();
            Assert.IsNotNull(checkme);
            Assert.Between<int>(checkme, min, max);

        }

        [Test]
        public void ListGeneratorTest()
        {
            IList<object> testlist = new List<object>();
            testlist.Add("hey");
            testlist.Add("yay");
            testlist.Add("nay");
            IGenerator listgen = new ListGenerator(testlist);
            string checkme = (string)listgen.Generate();
            Assert.IsNotNull(checkme);
            Assert.IsTrue(testlist.Contains(checkme));
        }

        [Test]
        public void NullGeneratorTest()
        {
            IGenerator nullgen = new NullGenerator();
            string checkme = (string)nullgen.Generate();
            Assert.IsNull(checkme);
        }

        private bool CheckPhone(string phonetocheck)
        {
            Regex phonepattern = new Regex("^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$");
            bool boolval = phonepattern.IsMatch(phonetocheck);
            return boolval;
        }

        [Test]
        public void AmericanPhoneGeneratorTest()
        {
            IGenerator phonegen = new AmericanPhoneGenerator();
            string phone = (string)phonegen.Generate();
            Assert.IsNotNull(phone);
            Assert.IsTrue(CheckPhone(phone));
        }

        private bool IsAmericanPostalCodeValid(string postalcode)
        {
            Regex postalcodepattern = new Regex("^\\d{5}$|^\\d{5}-\\d{4}$");
            return postalcodepattern.IsMatch(postalcode);
        }

        [Test]
        public void AmericanPostalCodeGenerator()
        {
            IGenerator postalgen = new AmericanPostalCodeGenerator(1);
            string zipcode = (string)postalgen.Generate();
            Assert.IsNotNull(zipcode);
            Assert.IsTrue(IsAmericanPostalCodeValid(zipcode));


        }

        [Test]
        public void TextGeneratorTest()
        {
            IGenerator textgen = new TextGenerator();
            string text = (string)textgen.Generate();
            Assert.IsNotNull(text);

        }

        
        [Test]
        public void AmericanAddressGeneratorTest()
        {
            IGenerator americanaddy = new AmericanAddressGenerator();
            string address = (string)americanaddy.Generate();
            Assert.IsNotNull(address);
        }

        
        [Test]
        public void AmericanCityGeneratorTest()
        {
            IGenerator americancity = new AmericanCityGenerator();
            string city=(string)americancity.Generate();
            Assert.IsNotNull(city);
        }

        [Test]
        public void AmericanStateGeneratorTest()
        {
            IGenerator americanstate = new AmericanStateGenerator();
            string state = (string)americanstate.Generate();
            Assert.IsNotNull(state);
        }


    }
}
